package cn.itcast.bookmanager.jframe;

import cn.itcast.bookmanager.dao.UserDao;
import cn.itcast.bookmanager.model.User;
import cn.itcast.bookmanager.util.JdbcUtils;
import cn.itcast.bookmanager.util.ToolUtils;
import com.mysql.jdbc.Connection;
import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginFrameEn extends JFrame {
	public static User currentUser;
	private JFrame jf;
	private JTextField userNameText;
	private JTextField passwordText;
	private JComboBox<String> comboBox;

	UserDao userDao = new UserDao();
	JdbcUtils dbUtil = new JdbcUtils();
	public LoginFrameEn(){
				
		jf=new JFrame("Picture Management");
		jf.getContentPane().setFont(new Font("幼圆", Font.BOLD, 14));
		jf.setBounds(600, 250, 500, 467);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.getContentPane().setLayout(null);

		JLabel lblNewLabel = new JLabel(new ImageIcon("lib\\tupian\\bg2.png"));
		lblNewLabel.setBounds(24, 10, 430, 218);
		jf.getContentPane().add(lblNewLabel);
		
		JLabel label = new JLabel("User：");
		label.setFont(new Font("幼圆", Font.BOLD, 14));
		label.setBounds(129, 250, 60, 29);
		jf.getContentPane().add(label);
		
		userNameText = new JTextField();
		userNameText.setBounds(199, 252, 127, 25);
		jf.getContentPane().add(userNameText);
		userNameText.setColumns(10);
		
		JLabel label_1 = new JLabel("Password：");
		label_1.setFont(new Font("幼圆", Font.BOLD, 14));
		label_1.setBounds(99, 289, 90, 29);
		jf.getContentPane().add(label_1);
		
		passwordText = new JPasswordField();
		passwordText.setColumns(10);
		passwordText.setBounds(199, 291, 127, 25);
		jf.getContentPane().add(passwordText);
		

		JButton button = new JButton("Login");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				checkLogin(e);
			}
		});
		button.setBounds(153, 377, 65, 29);
		jf.getContentPane().add(button);
		
		JButton button_1 = new JButton("Register");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				regUser(e);
			}
		});
		button_1.setBounds(263, 377, 80, 29);
		jf.getContentPane().add(button_1);

		//选择语言
		JButton Language = new JButton("简体中文");
		Language.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Language(e);
			}
		});
		Language.setBounds(405, 5, 80, 25);
		jf.getContentPane().add(Language);

		JLabel lblNewLabel_1 = new JLabel("");
		lblNewLabel_1.setIcon(new ImageIcon(LoginFrameEn.class.getResource("/tupian/adBG3.png")));
		lblNewLabel_1.setBounds(0, 0, 484, 429);
		jf.getContentPane().add(lblNewLabel_1);
		
		jf.setVisible(true);
		jf.setResizable(true);
		
	}
	protected void regUser(ActionEvent e) {
		jf.setVisible(false);
		new RegFrameEn();
		
	}
	protected void Language(ActionEvent e) {
		jf.setVisible(false);
		new LoginFrame();

	}
	protected void checkLogin(ActionEvent e) {
		String userName = userNameText.getText();
		String password = passwordText.getText();
		int index = 1; //缺省设置为普通用户
		if (ToolUtils.isEmpty(userName) || ToolUtils.isEmpty(password)) {
			JOptionPane.showMessageDialog(null, "User name and password cannot be empty");
			return;
		}
		User user = new User();
		user.setUserName(userName);
		user.setPassword(password);

		//System.out.println(user);
		Connection con = null;
		try {
			con = dbUtil.getConnection();
			User loginUser = userDao.login(con, user);
			System.out.println("login :"+loginUser);
			currentUser = loginUser;
			if (loginUser == null) {
				JOptionPane.showMessageDialog(null, "Login failed");
			} else {

					jf.dispose();
				    new UserFrameEn();
			}
		} catch (Exception e21) {

			e21.printStackTrace();
			JOptionPane.showMessageDialog(null, "Login exception");
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e31) {

				e31.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		try {
			BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.generalNoTranslucencyShadow;
			BeautyEyeLNFHelper.launchBeautyEyeLNF();
		} catch (Exception e) {
			e.printStackTrace();
		}
		new LoginFrameEn();
	}
}
